package com.coolw.spring.statemachine.support;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.persist.DefaultStateMachinePersister;
import org.springframework.statemachine.persist.StateMachinePersister;

import javax.annotation.Resource;

/**
 * 状态机持久化配置
 *
 * @author coolw
 * @date 2024/4/8 17:51
 */
@Slf4j
@Configuration
public class PersistConfig<S extends BaseState, E extends BaseEvent> {

    //@Resource
    //private RedisConnectionFactory redisConnectionFactory;

    @Resource
    private MemoryStateMachinePersist<S, E> memoryStateMachinePersist;

    /**
     * 内存持久化
     */
    @Bean("stateMachineMemPersister")
    public StateMachinePersister<S, E, String> getPersister() {
        return new DefaultStateMachinePersister<>(memoryStateMachinePersist);
    }

    /**
     * redis持久化
     */
    /*@Bean(name = "stateMachineRedisPersister")
    public RedisStateMachinePersister<S, E> getRedisPersister() {
        RedisStateMachineContextRepository<S, E> repository = new RedisStateMachineContextRepository<>(redisConnectionFactory);
        RepositoryStateMachinePersist<S, E> persist = new RepositoryStateMachinePersist<>(repository);
        return new RedisStateMachinePersister<>(persist);
    }*/
}
